<template>
    <div style="background-color: #fff;min-height: 500px;">
        <a-tabs default-active-key="1">
          <a-tab-pane key="1" tab="通用设置" force-render>
            <a-form-model ref="form1" :model="model" :rules="validatorRules">
              <a-row>
                <a-col :span="24">
                  <a-form-model-item label="系统名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="systemName">
                    <a-input v-model="model.systemName" placeholder="请输入系统名称"  ></a-input>
                  </a-form-model-item>
                </a-col>
                <a-col :span="24">
                  <a-form-model-item label="公司名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="companyName">
                    <a-input v-model="model.companyName" placeholder="请输入公司名称"  ></a-input>
                  </a-form-model-item>
                </a-col>
                <a-col :span="24">
                  <a-form-model-item label="公司Logo" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="companyLogo">
                    <j-image-upload isMultiple  v-model="model.companyLogo" ></j-image-upload>
                  </a-form-model-item>
                </a-col>
                <a-col :span="24">
                  <a-form-model-item label="时区设置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="timeZone">
                    <j-dict-select-tag type="list" v-model="model.timeZone" dictCode="time_zone" placeholder="请选择时区设置" />
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row style="margin-bottom: 0;">
                <a-col :span="5" style="text-align: right;">
                  <a-button  type="primary" @click="submitForm1">
                    保存设置
                  </a-button>
                </a-col>
              </a-row>
            </a-form-model>
          </a-tab-pane>
          <a-tab-pane key="2" tab="安全设置" force-render>
            <a-form-model ref="form2" :model="model" :rules="validatorRules">
              <a-row>
                <a-col :span="24">
                  <a-form-model-item label="密码策略" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="passwordStrategy">
                    <j-dict-select-tag type="list" v-model="model.passwordStrategy" dictCode="password_strategy" placeholder="请选择密码策略" />
                  </a-form-model-item>
                </a-col>
                <a-col :span="24">
                  <a-form-model-item label="会话超时时间（单位：分钟）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sessionTimeout">
                    <a-input-number v-model="model.sessionTimeout" :precision="0" placeholder="请输入会话超时时间（单位：分钟）" style="width: 100%" />
                  </a-form-model-item>
                </a-col>
                <a-col :span="24">
                  <a-form-model-item label="登录失败处理" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="loginFailLimit">
                    <j-dict-select-tag type="list" v-model="model.loginFailLimit" dictCode="login_fail_limit" placeholder="请选择登录失败处理" />
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row style="margin-bottom: 0;">
                <a-col :span="5" style="text-align: right;">
                  <a-button  type="primary" @click="submitForm2">
                    保存设置
                  </a-button>
                </a-col>
              </a-row>
            </a-form-model>
          </a-tab-pane>
          <a-tab-pane key="3" tab="数据管理">
            <a-form-model ref="form3" :model="model" :rules="validatorRules">
              <a-row>
                <a-col :span="24">
                  <a-form-model-item label="备份时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="backupTime">
                    <j-dict-select-tag type="list" v-model="model.backupTime" dictCode="backupTime" placeholder="请选择备份时间" />
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row style="margin-bottom: 0;">
                <a-col :span="8" style="text-align: right;">
                  <a-button  type="primary" @click="submitForm3">
                    保存设置
                  </a-button>
                  <a-popconfirm title="确定手动备份数据库吗?"  style="margin-left: 10px;" @confirm="() => backupDatabase()">
                    <a-button  type="primary">
                      立即备份数据库
                    </a-button>
                  </a-popconfirm>
                </a-col>
              </a-row>
            </a-form-model>
          </a-tab-pane>
        </a-tabs>
    </div>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'AssetSettingForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          systemName: [{required: true, message: '请输入系统名称!'}],
          companyName: [{required: true, message: '请输入公司名称!'}],
          companyLogo: [{required: true, message: '请上传公司Logo!'}],
          timeZone: [{required: true, message: '请选择时区设置!'}],
          passwordStrategy: [{required: true, message: '请选择密码策略!'}],
          sessionTimeout: [{required: true, message: '请输入会话超时时间!'}],
          loginFailLimit: [{required: true, message: '请选择登录失败处理!'}],
          backupTime: [{required: true, message: '请选择备份时间!'}],
        },
        url: {
          add: "/asset/assetSetting/add",
          edit: "/asset/assetSetting/edit",
          queryById: "/asset/assetSetting/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
      this.getData();
    },
    methods: {
      getData () {
        getAction(this.url.queryById+'?id=1').then((res)=>{
          if(res.success){
            this.model = res.result;
          }else{
            this.$message.warning(res.message);
          }
        })
      },
      submitForm1 (type) {
        const that = this;
        // 触发表单验证
        this.$refs.form1.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let data = {
              id: this.model.id,
              systemName: this.model.systemName,
              companyName: this.model.companyName,
              companyLogo: this.model.companyLogo,
              timeZone: this.model.timeZone
            }
            httpAction(httpurl,data,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                this.getData();
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
        })
      },
      submitForm2 () {
        const that = this;
        // 触发表单验证
        this.$refs.form2.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let data = {
              id: this.model.id,
              passwordStrategy: this.model.passwordStrategy,
              sessionTimeout: this.model.sessionTimeout,
              loginFailLimit: this.model.loginFailLimit
            }
            httpAction(httpurl,data,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                this.getData();
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
        })
      },
      submitForm3 () {
        const that = this;
        // 触发表单验证
        this.$refs.form3.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let data = {
              id: this.model.id,
              backupTime: this.model.backupTime
            }
            httpAction(httpurl,data,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                this.getData();
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
        })
      },
      backupDatabase(){
        const that = this;
        that.confirmLoading = true;
        httpAction('/asset/assetSetting/backupDatabase',{},'post').then((res)=>{
          if(res.success){
            that.$message.success(res.message);
          }else{
            that.$message.warning(res.message);
          }
        }).finally(() => {
          that.confirmLoading = false;
        })
      }
    }
  }
</script>